package com.ruoyi.service.jpjs.socialpayment.impl;


import com.ruoyi.mapper.jpjs.socialpayment.SocialPaymentMapper;
import com.ruoyi.service.jpjs.PublicService;
import com.ruoyi.service.jpjs.socialpayment.SocialPaymentService;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SocialPaymentServiceimpl implements SocialPaymentService {


    @Autowired
    SocialPaymentMapper socialPaymentMapper;

    @Autowired
    PublicService publicService;

    @Override
    public List<Map<String, Object>> list(Map map) {
        List<Map<String, Object>> compList = publicService.findUserCompAndJszb();
        map.put("compList", compList);
        return socialPaymentMapper.list(map);
    }

    @Override
    public int add(Map map) {
        return socialPaymentMapper.add(map);
    }

    @Override
    public Map<String, Object> findDataById(String id) {
        return socialPaymentMapper.findDataById(id);
    }

    @Override
    public int edit(Map map) {
        return socialPaymentMapper.edit(map);
    }

    @Override
    public int remove(String ids) {
        String[] idSplit = ids.split(",");
        int num = 0;
        for (String id : idSplit) {
            num += socialPaymentMapper.remove(id);
        }
        return num;
    }

    @Override
    public BaseResult getLastMonthData(Map map) throws Exception {
        //拼接成日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String dataStr = map.get("years").toString() + "-" + map.get("months").toString();
        System.out.println(dataStr);
        Date date = format.parse(dataStr);
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        // 设置为上一个月
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        date = calendar.getTime();
        dataStr = format.format(date);

        String[] array = dataStr.split("-");
        int years = Integer.parseInt(array[0]);
        int months = Integer.parseInt(array[1]);
        Map map1 = socialPaymentMapper.getLastMonthData(months, years, map.get("compId").toString());
        if (map1 != null) {
            return BaseResult.build(ResultEnum.SUCCESS, map1);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }
}
